#include "homepage.h"
#include "ui_homepage.h"

HomePage::HomePage(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(btnClickButton()));
    data = new DataInfo;

    connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(comxClickSlot(int)));

}

HomePage::~HomePage()
{
    delete ui;
}

void HomePage::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap map(":/new/prefix1/res/1.JPG");
    painter.drawPixmap(0,0,this->width(),this->height(),map);
}


void HomePage::keyValueGetSlot(int key, QString val)
{
    switch (key) {
    case 200:
        ui->label_22->setText(val);
        break;
    case 201:
        ui->label_23->setText(val);
        break;
    case 202:
        ui->label_39->setText(val);
        break;
    case 203:
        if(val == "1")
        {
            ui->pushButton_zhe->setText("开");

        }else if(val == "0")
        {
            ui->pushButton_zhe->setText("关");
        }
        break;
    case 204:
        if(val == "1")
        {
            ui->pushButton_jia->setText("开");

        }else if(val == "0")
        {
            ui->pushButton_jia->setText("关");
        }
        break;
    case 205:
        if(val == "1")
        {
            ui->pushButton_ac->setText("开");

        }else if(val == "0")
        {
            ui->pushButton_ac->setText("关");
        }
        break;
    case 300:
        ui->label->setText(val);
        break;
    case 301:
        ui->label_3->setText(val);
        break;
    case 302:
        ui->label_4->setText(val);
        break;
    case 303:
        ui->label_29->setText(val);
        break;
    case 304:
        ui->label_30->setText(val);
        break;
    }
}

void HomePage::dataUpSlot(QByteArray msg)
{
    data->inparse(msg);
}

void HomePage::btnClickButton()
{
    QJsonObject json;
    json.insert("type",1);
    data->publishMessage(json);
}

void HomePage::comxClickSlot(int index)
{
    QJsonObject json;
    QJsonObject ctljson;
    json.insert("type",3);
    switch (index) {
    case 0:
        ctljson.insert("type",1);
        ctljson.insert("period",5);
        break;
    case 1:
        ctljson.insert("type",2);
        ctljson.insert("period",5);
        break;
    case 2:
        ctljson.insert("type",3);
        ctljson.insert("period",ui->lineEdit->text().toInt());
        break;

    }
    json.insert("data",ctljson);
    data->publishMessage(json);
}




